package stu.sup.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import stu.sup.entity.UserLogin;
import stu.sup.service.IUserLoginService;
import stu.sup.util.JsonResult;
import stu.sup.util.Sha256Utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 本类用来演示：
 *
 * @author:Administrator
 * @data:2020/7/2 0002
 */
@Controller
public class UserLoginController {

    @Autowired
    private IUserLoginService userLoginService;

    @RequestMapping("login")
    public String login(){
        return "/view/login";
    }

    @PostMapping("/view/login")
    @ResponseBody
    public JsonResult loginByUserNameAndPwd(String username,String password,Integer kinds, HttpServletRequest request){
        System.out.println("输入的userName:"+username);
        System.out.println("输入的password:"+password);
        System.out.println("输入的kinds:"+kinds);

        //判断是商家还是用户
        String status = null;
        if (kinds == 1){
            status = "1";
        }else {
            status = "0";
        }

        //判断是否登录成功
        JsonResult jsonResult = userLoginService.loginByUserAndPwd(username,password,status);

        UserLogin userLogin = (UserLogin) jsonResult.getData();

        if (jsonResult.getCode() == 1){//用户不存在
            System.out.println("用户名不存在");
            return new JsonResult(2,"不存在",userLogin);
        }else {
            System.out.println("用户存在");

            request.getSession().setAttribute("loginId",userLogin.getId());//将登录账号id放入作用域
            request.getSession().setAttribute("loginUserName",userLogin.getUsername());//将登录账号用户名放入作用域
            request.getSession().setAttribute("loginTel",userLogin.getTelephone());//将登录账号电话放入作用域

            //用户
            if (kinds == 1)
                return new JsonResult(1,"用户存在",userLogin);
            else//商家
                return new JsonResult(0,"商家存在登录",userLogin);
        }
    }



}
